{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 环境配置"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "pip install fschat accelerate\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 服务开启"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "latex"
    }
   },
   "outputs": [],
   "source": [
    "\n",
    "# 方式一\n",
    "\n",
    "# 启动控制器\n",
    "python -m fastchat.serve.controller --host 0.0.0.0 --port 21003\n",
    "\n",
    "# 为模型工作进程设置环境变量并启动\n",
    "python -m fastchat.serve.model_worker --model-path /root/autodl-tmp/Qwen/Qwen3-8B --model-names qwen3-8b --num-gpus 1 --controller-address http://127.0.0.0:21003\n",
    "\n",
    "# 启动openai API服务器\n",
    "python -m fastchat.serve.openai_api_server --host 0.0.0.0 --port 6006 --controller-address http://127.0.0.1:21003\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 如果遇到了错误\n",
    "\n",
    "error while attempting to bind on address ('::1', 21002, 0, 0): cannot assign requested address"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "latex"
    }
   },
   "outputs": [],
   "source": [
    "sudo vi sources.list\n",
    "\n",
    "复制以下内容到sources.list中\n",
    "deb http://mirrors.aliyun.com/ubuntu/ trusty main multiverse restricted universe\n",
    "deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main multiverse restricted universe\n",
    "deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main multiverse restricted universe\n",
    "deb http://mirrors.aliyun.com/ubuntu/ trusty-security main multiverse restricted universe\n",
    "deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main multiverse restricted universe\n",
    "deb-src http://mirrors.aliyun.com/ubuntu/ trusty main multiverse restricted universe\n",
    "deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main multiverse restricted universe\n",
    "deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main multiverse restricted universe\n",
    "deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main multiverse restricted universe\n",
    "deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main multiverse restricted universe\n",
    "\n",
    "sudo apt-get update\n",
    "sudo apt upgrade\n",
    "\n",
    "\n",
    "sudo apt-get install net-tools\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "latex"
    }
   },
   "outputs": [],
   "source": [
    "\n",
    "# 方式二\n",
    "\n",
    "# 启动控制器\n",
    "python -m fastchat.serve.controller --host 0.0.0.0 --port 21003 \n",
    "\n",
    "# 为模型工作进程设置环境变量并启动\n",
    "python -m fastchat.serve.model_worker --model-path /root/autodl-tmp/Qwen/Qwen3-8B --model-names qwen3-8b --num-gpus 1 --controller-address http://172.17.0.5:21003 --worker-address http://172.17.0.5:8080 --host 0.0.0.0 --port 8080\n",
    "\n",
    "# 启动openai API服务器\n",
    "python -m fastchat.serve.openai_api_server --controller-address http://172.17.0.5:21003 --host 0.0.0.0 --port 6006"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## openai服务"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<think>\n",
      "好的，用户问我是谁以及我的名字。首先，我需要确认自己的身份，作为通义千问，我应该明确回答用户的问题。用户可能是在测试我的反应，或者想了解我的功能。接下来，我需要确保回答简洁明了，同时符合公司的规定和指导原则。在回答时，我会提供我的名字和功能，同时保持专业和友好的态度。此外，用户可能希望了解更多关于我的能力，因此我会在回答中加入邀请用户提问的部分，以促进进一步的交流。最后，我会检查回答是否符合所有要求，确保没有错误或遗漏。\n",
      "</think>\n",
      "\n",
      "我是通义千问，由通义实验室研发。我是一个大型语言模型，能够回答各种问题，提供帮助和信息。如果您有任何问题或需要帮助，请随时告诉我！\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from openai import OpenAI\n",
    "\n",
    "# client = OpenAI(\n",
    "#     # 若没有配置环境变量，请用百炼API Key将下行替换为：api_key=\"sk-xxx\",\n",
    "#     api_key=os.getenv(\"DASHSCOPE_API_KEY\"), # 如何获取API Key：https://help.aliyun.com/zh/model-studio/developer-reference/get-api-key\n",
    "#     base_url=\"https://dashscope.aliyuncs.com/compatible-mode/v1\",\n",
    "# )\n",
    "\n",
    "client = OpenAI(\n",
    "   \n",
    "    api_key=\"na\", \n",
    "    base_url=\"http://localhost:8000/v1\",\n",
    ")\n",
    "\n",
    "\n",
    "completion = client.chat.completions.create(\n",
    "    model=\"qwen3-8b\", \n",
    "    messages=[\n",
    "        {'role': 'system', 'content': '你是一个有用的聊天助手'},\n",
    "        {'role': 'user', 'content': '你是谁，你叫什么名字'}\n",
    "        ]\n",
    ")\n",
    "print(completion.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ame",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
